﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.IO;


namespace Ucb.Workshop.Dal
{
    public class DBManager
    {
        private static string Scadena;
        private static SqlConnection Oconexion;

        public DBManager()
        {

            Scadena = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\BDInventory.mdf;Integrated Security=True;User Instance=True";
        }

        public string GetCadenaConexion()
        {
            return Scadena;
        }

        public static SqlConnection Conexion()
        {
            try
            {
                Oconexion = new SqlConnection(Scadena);
                return Oconexion;
            }
            catch (SqlException rff)
            {
                Console.WriteLine("MENSAJE DE ERROR EN LA CONEXION" + rff);
                return null;
            }

        }


        public bool EjecutarConsulta(string Sconsulta, string Smensaje)
        {
            SqlConnection oConexion = Conexion();
            oConexion.Open();
            try
            {
                SqlCommand oComando = oConexion.CreateCommand();
                oComando.CommandText = Sconsulta;
                SqlDataReader oDataReader = oComando.ExecuteReader();
                oConexion.Close();
               MessageBox.Show(Smensaje);
                return true;

            }
            catch //(SqlException e)
            {
                //MessageBox.Show(e.ToString());
                //MessageBox.Show("Error en la Base de Datos!!!"+"\n"+e);
                return false;
            }

        }

        public bool EjecutarConsulta(string Sconsulta)
        {
            SqlConnection Oconexion = Conexion();
            Oconexion.Open();
            try
            {
                SqlCommand oComando = Oconexion.CreateCommand();
                oComando.CommandText = Sconsulta;
                SqlDataReader oDataReader = oComando.ExecuteReader();
                Oconexion.Close();
                return true;

            }
            catch (SqlException e)
            {
                MessageBox.Show(e.ToString());
                return false;
            }
            
        }

        public void LlenarGrid(DataGridView OdataGridView, string Sconsulta)
        {
            SqlConnection oConexion = Conexion();


            SqlDataAdapter oDataAdapter = new SqlDataAdapter();
            oDataAdapter.TableMappings.Add("Tabla", "BD");

            oConexion.Open();

            SqlCommand cmd = new SqlCommand((string)Sconsulta, (SqlConnection)oConexion);
            cmd.CommandType = CommandType.Text;
            oDataAdapter.SelectCommand = cmd;

            DataSet oDataSet = new DataSet("Tabla");
            oDataAdapter.Fill(oDataSet);

            OdataGridView.DataSource = oDataSet.Tables[0];
        }

        public SqlDataReader ExeConsulta(String Consulta)
        {
            SqlConnection connection = Conexion();  
            connection.Open();  

            try
            {
               

                SqlCommand dbcmd = connection.CreateCommand();
                dbcmd.CommandText = Consulta;

                SqlDataReader reader = dbcmd.ExecuteReader();
                //connection.Close();

                return reader;
                //MessageBox.Show(mensaje, "Consulta Ejecutada", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (SqlException e)
            {
                MessageBox.Show("ERROR: AL EJECUTAR CONSULTA - " + e);
                return null;
                //MessageBox.Show(mensaje, "Error al ejecutar la consulta", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        public DataSet ConsultaDataSet(string query)
        {

            DataSet myDataSet = new DataSet();
            SqlConnection connection = Conexion();
            connection.Open();

            try
            {
                SqlCommand mySelectCommand = new SqlCommand(query, connection);//se crea el comando con ls consulta
                SqlDataAdapter myDataAdapter = new SqlDataAdapter();
                myDataAdapter.SelectCommand = mySelectCommand;//asignacion de un selectcommand
                myDataAdapter.Fill(myDataSet);//se llena el DataSet
                // connection.Close();

            }
            finally
            {
                connection.Close();
            }
            return myDataSet;
        }


    }
}
